package m.jclecai.com.adapter.base;

import android.content.Context;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.adapter.holder.DefaultViewHolder;

/**
 * Created by Dinosa on 2017/11/21.
 * 这种封装的缺点就是不支持多类型的条目；
 */

public abstract class DefaultViewHolderAdapter<T>  extends  ViewHolerAdapter<DefaultViewHolder<T>> {


    public List<T> mData=new ArrayList<>();
    public Context mContext;

    public DefaultViewHolderAdapter(Context context) {
        mContext = context;
    }

    @Override
    public DefaultViewHolder<T> onCreateViewHolder(int position, ViewGroup parent) {
        return getRealViewHolder(mContext);
    }

    @Override
    public void onBindViewHolder(int position, DefaultViewHolder<T> tDefaultViewHolder) {
        tDefaultViewHolder.init(mData.get(position),position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    /**
     * 创建一个默认的真正的ViewHolder
     * @return
     */
    public abstract   DefaultViewHolder<T> getRealViewHolder (Context context);

    public void clear(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refresh(List<T> data){
        clear();
        addAll(data);
    }

    public void addAll(List<T> data){
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void add(T data){
        mData.add(data);
        notifyDataSetChanged();
    }

    public void del(T data){
        mData.remove(data);
        notifyDataSetChanged();
    }
}
